#!/bin/bash

BinHome="/opt/server.lan"
MntHome="/mnt/server.lan"
TmpHome="/tmp/server.lan"


#######################################
# /tmp 
tmpString='tmpfs  /tmp  tmpfs  defaults 0 0'
if ! grep -E "^$tmpString" /etc/fstab &> /dev/null;then
    echo "$tmpString" | tee -a /etc/fstab
fi


#######################################

# dirs
mkDirIfNotExist(){
    if [ -z "$1" ] || [ -d "$1" ] ;then
        return
    fi
    
    if [ -e "$1" ];then 
        echo "[E] not dir error :: $1"
        return
    fi 

    mkdir "$1"

}

mkDirIfNotExist "$MntHome"

mkDirIfNotExist "$BinHome"
mkDirIfNotExist "$BinHome/www"
mkDirIfNotExist "$BinHome/www/data"
mkDirIfNotExist "$BinHome/www/homepage"
mkDirIfNotExist "$BinHome/ssl"



mkLink(){
    if [ -z "$2" ];then 
        return
    fi

    if [ -h "$2" ];then
        rm "$2"
    fi

    if [ -e "$2" ] ;then
        echo "[E] softLink can't be created, (a file|dir is already existed)"
        return
    fi
    ln -s "$1" "$2" 
}

#
mkLink  "$MntHome" "$BinHome/disks"
if [ "x$DIR_NOT_CHECK_MOUNTPOINT" == "xtrue" ];then
    date > "$BinHome/disks/NO_CHECK_MOUNTPOINT"
fi

mkLink  "$TmpHome" "$BinHome/tmp" 

# ca ## tls: .key .crt
mkLink "$BinHome/ssl/server.latest.key.pem" "${BinHome}/server.key"
mkLink "$BinHome/ssl/server.latest.crt.pem" "${BinHome}/server.crt"
mkLink "$BinHome/ssl/root_ca.crt.pem" "${BinHome}/root_ca.crt"
mkLink "$BinHome/ssl/root_ca.crt.pem" "${BinHome}/www/root_ca.crt"

#######################################


# disks

genMountText(){
    # $1:label
    # $2: path
    # $3: dir
    ## 1 mkdir
    if [ ! -e "$2/$3" ];then
        mkdir -p "$2/$3"
    fi

    if [ "x$DIR_NOT_CHECK_MOUNTPOINT" == "xtrue" ];then
        echo "[INFO] DIR_NOT_CHECK_MOUNTPOINT is setted, so no disk will be mounted"
    else
        ## 2 mount
        if [ ! -e "/dev/disk/by-label/$1" ];then
            echo "[Err] /dev/disk/by-label/$1 : not exist"
            exit 1
        fi

        ## 4 /etc/fstab
        echo "LABEL=$1     $2/$3      ext4  defaults,nofail  0  2" | tee -a /etc/fstab
        #echo "[Warn] edit /etc/fstab && remove # "

        # mount -> mkdir -> chown
        mount /dev/disk/by-label/$1 "$2/$3"
        if ! mountpoint "$2/$3";then
            echo "[E] can't mount disk: $1"
            echo "    mount /dev/disk/by-label/$1 \"$2/$3\""
            return
        fi
    fi

    if [ -e "$2/$3/$3Storage" ] && [ ! -d "$2/$3/$3Storage" ];then
        echo "[E] path exists but is not a dir :: $2/$3/$3Storage
        _________________________________________________
        "
        return
    fi
    if [ ! -d "$2/$3/$3Storage" ];then
        mkdir "$2/$3/$3Storage"
    fi
    chown 1000:1000 "$2/$3" "$2/$3/${3}Storage"
}

genMountText_post(){
    if [ "x$DIR_NOT_CHECK_MOUNTPOINT" == "xtrue" ];then
        return
    fi

    {
        echo ""
        echo "$tmpString `date +%Y/%m/%d-%H:%M:%S`" 
    } | tee -a /etc/fstab
    
    mount -a
}



tmpString="#server.lan ## mount ### DONE "
if ! grep -E "^$tmpString" /etc/fstab &> /dev/null;then
    # disk :: ## () -> mkfs.ext4 -> e2label
    genMountText disk_data   "$MntHome" data 
    genMountText disk_backup "$MntHome" backup

    genMountText_post
else
    mount -a
fi


mkDirIfNotExist "$TmpHome"
# "$BinHome/disks/data/dataStorage"
#######################################

echo "#######################################
[INFO] 
    other disks need ::
        'edit /etc/fstab' and 'mount -a'
        make links  into  '$MntHome/data'
#######################################
"

